﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;
using MINERP.COM;

using System.Drawing.Imaging;
using MINERP.COM.Barcode;

namespace ImageCom.LeadtoolEx
{
    public abstract class LtBase
    {
        protected LtBase(string inpath)
        {

            InitColor();

            _runFolder = inpath;
            if (!Directory.Exists(_runFolder))
            {
                Directory.CreateDirectory(_runFolder);
            }
        }
        /// <summary>
        /// 规范的A3+大小
        /// </summary>
        protected Size _a3PlugsSize = new Size(320, 464);
        /// <summary>
        /// 默认的DPI
        /// </summary>
        protected const int Dpi = 254;
        /// <summary>
        /// 运行目录
        /// </summary>
        protected string _runFolder = "";
        /// <summary>
        /// 颜色列表
        /// </summary>
        readonly List<RasterColor> _colorList = new List<RasterColor>();
        private const int ColorWidth = 50;
        private string _barcodeName = "Code 128";

        private const int wucha = 20;//110 个像素
        private void InitColor()
        {
            _colorList.Add(new RasterColor(0, 0, 0));
            _colorList.Add(new RasterColor(192, 192, 192));
            _colorList.Add(new RasterColor(128, 128, 105));
            _colorList.Add(new RasterColor(255, 255, 255));
            _colorList.Add(new RasterColor(250, 235, 215));
            _colorList.Add(new RasterColor(240, 255, 255));
            _colorList.Add(new RasterColor(245, 245, 245));
            _colorList.Add(new RasterColor(255, 235, 205));
            _colorList.Add(new RasterColor(252, 230, 210));
            _colorList.Add(new RasterColor(255, 255, 0));
            _colorList.Add(new RasterColor(227, 207, 87));
            _colorList.Add(new RasterColor(255, 153, 18));

            _colorList.Add(new RasterColor(255, 215, 0));
            _colorList.Add(new RasterColor(218, 165, 105));
            _colorList.Add(new RasterColor(255, 97, 0));
            _colorList.Add(new RasterColor(237, 145, 33));
            _colorList.Add(new RasterColor(245, 222, 179));
            _colorList.Add(new RasterColor(176, 224, 230));
            _colorList.Add(new RasterColor(65, 105, 225));

            _colorList.Add(new RasterColor(135, 206, 235));

            _colorList.Add(new RasterColor(0, 255, 255));
            _colorList.Add(new RasterColor(56, 94, 15));


            _colorList.Add(new RasterColor(8, 46, 84));
            _colorList.Add(new RasterColor(64, 224, 208));

            _colorList.Add(new RasterColor(0, 255, 0));
            _colorList.Add(new RasterColor(61, 145, 64));
            _colorList.Add(new RasterColor(255, 250, 240));
            _colorList.Add(new RasterColor(0, 201, 87));

            _colorList.Add(new RasterColor(220, 220, 220));
            _colorList.Add(new RasterColor(250, 240, 230));
            _colorList.Add(new RasterColor(255, 0, 0));
            _colorList.Add(new RasterColor(156, 102, 31));
            _colorList.Add(new RasterColor(227, 23, 13));

            _colorList.Add(new RasterColor(255, 127, 80));
            _colorList.Add(new RasterColor(176, 23, 31));
            _colorList.Add(new RasterColor(128, 42, 42));

            _colorList.Add(new RasterColor(163, 148, 128));
            _colorList.Add(new RasterColor(138, 54, 15));
            _colorList.Add(new RasterColor(210, 105, 30));

            _colorList.Add(new RasterColor(240, 230, 140));
            _colorList.Add(new RasterColor(188, 143, 143));

            _colorList.Add(new RasterColor(244, 164, 96));

            _colorList.Add(new RasterColor(0, 0, 255));
            _colorList.Add(new RasterColor(189, 252, 201));
            _colorList.Add(new RasterColor(46, 139, 87));

            _colorList.Add(new RasterColor(160, 32, 240));
            _colorList.Add(new RasterColor(160, 102, 211));

            _colorList.Add(new RasterColor(221, 160, 221));
            _colorList.Add(new RasterColor(160, 102, 211));
            _colorList.Add(new RasterColor(41, 36, 33));
            ///50个颜色
        }

        private Code128 _code128 = new Code128();

     

        /// <summary>
        /// 生成条码
        /// </summary>
        /// <param name="inOrderContent">订单内容逗号隔开</param>
        /// <param name="inPhotoContent">照片名</param>
        /// <param name="inWidth">宽度</param>
        /// <param name="inHeightMm">高度</param>
        /// <param name="inDpi">分辨率</param>
        /// <param name="isVertical">是否竖向</param>
        /// <param name="barcodeFont">条码字体</param>
        /// <param name="textFont">备注字体</param>
        /// <returns></returns>
        protected RasterImage GenerateTags(string inOrderContent, string inPhotoContent, int inWidthPix, int inHeightMm = 5, int inDpi = 254, bool isVertical = true, Font barcodeFont = null, Font textFont = null, int offset = 0)
        {
            if (string.IsNullOrEmpty(inOrderContent))
            {
                inOrderContent = "1234,测试产品";
            }
            if (barcodeFont == null)
            {
                barcodeFont = new Font("3 of 9 Barcode", 20);// new Font("微软雅黑",24))

            }
            if (textFont == null)
            {
                textFont = new Font("微软雅黑", 12);
            }

            int currXLocal = ConvertEx.Mm2Pix(offset, 254);//当前操作在备注栏上的相对位置
            //Font barcodeFont
            RasterImage destImage = null;
            string orderproductId = inOrderContent.Split('_')[0];
            RasterColor firstColor = new RasterColor(0, 0, 0);
            RasterColor secondColor = new RasterColor(0, 0, 0);

            int pixHeight = ConvertEx.Mm2Pix(inHeightMm, inDpi);
            int pixWidth = inWidthPix;
            int conLength = orderproductId.Length;
            destImage = CreateRasterImage(new Size(pixWidth, pixHeight));

            if (conLength >= 4)
            {
                string fistCom = orderproductId.Substring(conLength - 4, 1) + orderproductId.Substring(conLength - 2, 1);
                string secondCom = orderproductId.Substring(conLength - 3, 1) + orderproductId.Substring(conLength - 1, 1);
                int intFirst = Convert.ToInt32(fistCom);
                int intSecond = Convert.ToInt32(secondCom);
                int intFirstColor = intFirst % _colorList.Count;
                int intSecondColor = intSecond % _colorList.Count;
                firstColor = _colorList[intFirstColor];
                secondColor = _colorList[intSecondColor];
            }
            // string code128 = BarcodeConverter.ConvertToCode128(orderproductId);            \
            string barcode = "*" + orderproductId + "*";
            RasterImage firstColorImage = CreateRasterImage(new Size(ColorWidth, pixHeight), firstColor.R, firstColor.G,
                firstColor.B);
            RasterImage secondColorImage = CreateRasterImage(new Size(ColorWidth, pixHeight), secondColor.R, secondColor.G,
               secondColor.B);
            Graphics gDraw = destImage.CreateGdiPlusGraphics().Graphics;//用来打印条码和文字
            Rectangle fcRectangle = new Rectangle(currXLocal, 0, ColorWidth, pixHeight);
            currXLocal += ColorWidth;
            Rectangle scRectangle = new Rectangle(currXLocal, 0, ColorWidth, pixHeight);
            currXLocal += ColorWidth;
     
            currXLocal += 15;
            SizeF sizeF = gDraw.MeasureString(barcode, barcodeFont);
            int BARCODEWIDTH = Convert.ToInt32(sizeF.Width / gDraw.DpiX * inDpi) + 2;//条码的宽度
            Rectangle rcBarcode = new Rectangle(currXLocal, 0, BARCODEWIDTH, pixHeight);
            currXLocal += BARCODEWIDTH;
            currXLocal += 5;
            Rectangle rcFont = new Rectangle(currXLocal, -5, destImage.Width - currXLocal, pixHeight);
            gDraw.DrawString(barcode, barcodeFont, Brushes.Black, rcBarcode.X, rcBarcode.Y);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Near;
            sf.LineAlignment = StringAlignment.Near;

            gDraw.DrawString(inPhotoContent + inOrderContent, textFont, new SolidBrush(Color.Black), rcFont, sf);
            ColorResolutionCommand command = new ColorResolutionCommand(ColorResolutionCommandMode.InPlace, 24, RasterByteOrder.Bgr,
                                                               RasterDitheringMethod.None, ColorResolutionCommandPaletteFlags.Optimized, null);
            command.Run(destImage);//处理一遍条码和文字

            CombineImage(firstColorImage, destImage, fcRectangle);

            CombineImage(secondColorImage, destImage, scRectangle);
            if (isVertical)
            {
                RotateImage(destImage, 90);
            }

            return destImage;
        }
        protected RasterImage GenerateTag128(string inOrderContent, string inPhotoContent, int inWidthPix, RasterCodecs codes, int inHeightMm = 5, int inDpi = 254, bool isVertical = true, Font textFont = null, int offset = 0)
        {
            if (string.IsNullOrEmpty(inOrderContent))
            {
                inOrderContent = "1234,测试产品";
            }

            if (textFont == null)
            {
                textFont = new Font("微软雅黑", 12);
            }

            int currXLocal = ConvertEx.Mm2Pix(offset, 254);//当前操作在备注栏上的相对位置
            //Font barcodeFont
            RasterImage destImage = null;
            string orderproductId = inOrderContent.Split('_')[0];
            RasterColor firstColor = new RasterColor(0, 0, 0);
            RasterColor secondColor = new RasterColor(0, 0, 0);

            int pixHeight = ConvertEx.Mm2Pix(inHeightMm, inDpi);
            int pixWidth = inWidthPix;
            int conLength = orderproductId.Length;
            destImage = CreateRasterImage(new Size(pixWidth, pixHeight));

            if (conLength >= 4)
            {
                string fistCom = orderproductId.Substring(conLength - 4, 1) + orderproductId.Substring(conLength - 2, 1);
                string secondCom = orderproductId.Substring(conLength - 3, 1) + orderproductId.Substring(conLength - 1, 1);
                int intFirst = Convert.ToInt32(fistCom);
                int intSecond = Convert.ToInt32(secondCom);
                int intFirstColor = intFirst % _colorList.Count;
                int intSecondColor = intSecond % _colorList.Count;
                firstColor = _colorList[intFirstColor];
                secondColor = _colorList[intSecondColor];
            }
            // string code128 = BarcodeConverter.ConvertToCode128(orderproductId);            \
            // string barcode = "*" + orderproductId + "*";
            RasterImage firstColorImage = CreateRasterImage(new Size(ColorWidth, pixHeight), firstColor.R, firstColor.G,
                firstColor.B);
            RasterImage secondColorImage = CreateRasterImage(new Size(ColorWidth, pixHeight), secondColor.R, secondColor.G,
               secondColor.B);
            Graphics gDraw = destImage.CreateGdiPlusGraphics().Graphics;//用来打印条码和文字
            Rectangle fcRectangle = new Rectangle(currXLocal, 0, ColorWidth, pixHeight);
            currXLocal += ColorWidth;
            Rectangle scRectangle = new Rectangle(currXLocal, 0, ColorWidth, pixHeight);
            currXLocal += ColorWidth;
      
            currXLocal += 15;
            RasterImage codeImage = GenBarcode128(codes, orderproductId);
            //SaveRasteImage(codes,codeImage,@"E:\台历测试\测试\bar2.jpg");
            //SizeF sizeF = gDraw.MeasureString(barcode, barcodeFont);
            Rectangle codeFont = new Rectangle(currXLocal, -5, codeImage.Width, pixHeight);
            CombineImage(codeImage, destImage, codeFont);
            int BARCODEWIDTH = codeImage.Width + 3;//条码的宽度


            currXLocal += BARCODEWIDTH;
            currXLocal += 5;
            Rectangle rcFont = new Rectangle(currXLocal, -5, destImage.Width - currXLocal, pixHeight);

            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Near;
            sf.LineAlignment = StringAlignment.Near;

            gDraw.DrawString(inPhotoContent + inOrderContent, textFont, new SolidBrush(Color.Black), rcFont, sf);
            ColorResolutionCommand command = new ColorResolutionCommand(ColorResolutionCommandMode.InPlace, 24, RasterByteOrder.Bgr,
                                                               RasterDitheringMethod.None, ColorResolutionCommandPaletteFlags.Optimized, null);
            command.Run(destImage);//处理一遍条码和文字

            CombineImage(firstColorImage, destImage, fcRectangle);

            CombineImage(secondColorImage, destImage, scRectangle);
            if (isVertical)
            {
                RotateImage(destImage, 90);
            }

            return destImage;
        }

        protected void AddBarcode(string inOrderContent, string inPhotoContent, RasterImage srcImage, int inHeightMm = 5, int inDpi = 254, Font barcodeFont = null)
        {
            if (barcodeFont == null)
            {
                barcodeFont = new Font("3 of 9 Barcode", 20);// new Font("微软雅黑",24))

            }
            Graphics gDraw = srcImage.CreateGdiPlusGraphics().Graphics;//用来打印条码和文字
            string orderproductId = inOrderContent.Split('_')[0];
            string barcode = "*" + orderproductId + "*";
            SizeF sizeF = gDraw.MeasureString(barcode, barcodeFont);

            int BARCODEWIDTH = Convert.ToInt32(sizeF.Width / gDraw.DpiX * inDpi) + 2;//条码的宽度
            int currXLocal = srcImage.Width - 50 - BARCODEWIDTH;
            int pixHeight = ConvertEx.Mm2Pix(inHeightMm, inDpi);
            Rectangle rcBarcode = new Rectangle(currXLocal, srcImage.Height - pixHeight, BARCODEWIDTH, pixHeight);
            gDraw.DrawString(barcode, barcodeFont, Brushes.Black, rcBarcode.X, rcBarcode.Y);

        }

        public abstract void OnProcessing(string info, MsgType msgType, float percent);


        public RasterImage ExpandImageImg(RasterImage srcImage, RasterImage image, int expandMM, LocalDirection expandDir,
            bool footLine = false)
        {
            int imageWidth = image.Width;
            int imageHeigth = image.Height;
            int expandPix = Convert.ToInt32(expandMM / (10 * 2.54m) * image.XResolution);
            switch (expandDir)
            {
                case LocalDirection.Bottom:
                case LocalDirection.Top:
                    imageHeigth = imageHeigth + expandPix;
                    break;
                case LocalDirection.Left:
                case LocalDirection.Right:
                    imageWidth = imageWidth + expandPix;
                    break;
                default:
                    imageWidth = imageWidth + expandPix;
                    break;

            }
            if (LocalDirection.Top == expandDir)
            {
                return srcImage.Clone(new Rectangle(0, 0, imageWidth, imageHeigth));
            }
            else if (LocalDirection.Bottom == expandDir)
            {
                return srcImage.Clone(new Rectangle(0, image.Height - imageHeigth, imageWidth, imageHeigth));

            }
            else if (LocalDirection.Left == expandDir)
            {
                return srcImage.Clone(new Rectangle(image.Width - imageWidth, 0, imageWidth, imageHeigth));

            }
            else if (LocalDirection.Right == expandDir)
            {
                return srcImage.Clone(new Rectangle(0, 0, imageWidth, imageHeigth));
            }
            else
            {
                return srcImage.Clone(new Rectangle(0, 0, imageWidth, imageHeigth));
            }

        }
        /// <summary>
        /// 按照最边上的像素扩
        /// </summary>
        /// <param name="image"></param>
        /// <param name="expandMM"></param>
        /// <param name="expandDir"></param>
        /// <param name="footLine"></param>
        /// <returns></returns>
        public RasterImage ExpandImagePix(RasterImage image, int expandMM, LocalDirection expandDir, bool footLine = false)
        {


            int imageWidth = image.Width;
            int imageHeigth = image.Height;
            int expandPix = Convert.ToInt32(expandMM / (10 * 2.54m) * image.XResolution);
            switch (expandDir)
            {
                case LocalDirection.Bottom:
                case LocalDirection.Top:
                    imageHeigth = imageHeigth + expandPix;
                    break;
                case LocalDirection.Left:
                case LocalDirection.Right:
                    imageWidth = imageWidth + expandPix;
                    break;
                default:
                    imageWidth = imageWidth + expandPix;
                    break;
            }
            //image.PointToImage()
            RasterImage destImage = new RasterImage(
                 RasterMemoryFlags.Tiled,
                     imageWidth,
                     imageHeigth,
                     24,
                     RasterByteOrder.Bgr,
                     RasterViewPerspective.BottomLeft,
                     null,
                     IntPtr.Zero,
                     0);
            destImage.XResolution = destImage.YResolution = image.XResolution;//目标的分辨率
            destImage.OriginalFormat = RasterImageFormat.Jpeg;
            //destImage.PointToImage()
            FillCommand fcommand = new FillCommand();

            fcommand.Color = new RasterColor(255, 255, 255);
            fcommand.Run(destImage);
            ChangeViewPerspectiveCommand changeViewPerspectiveCommand = new ChangeViewPerspectiveCommand();
            RasterViewPerspective rasterViewPerspective = RasterViewPerspective.TopLeft;
            changeViewPerspectiveCommand.ViewPerspective = rasterViewPerspective;
            changeViewPerspectiveCommand.InPlace = true;
            RasterImageChangedFlags flag = changeViewPerspectiveCommand.Run(destImage);

            System.Drawing.Rectangle imageSrc = new System.Drawing.Rectangle(0, 0, image.Width, image.Height);///放原来照片的位置
            if (LocalDirection.Top == expandDir)
            {
                ///合并图案
                imageSrc.X = 0;
                imageSrc.Y = expandPix;
                CombineImage(image, destImage, imageSrc);
                //此时expandMM为高度
                //destImage图像的上面是复制出的图像
                //宽度0-imageWidth高度expandMM
                //复制元数据为第一行颜色
                for (int xP = 0; xP < image.Width; xP++)//列值
                {

                    //var DotaPix1 = image.GetPixelColor(image.Height - 1, xP);
                    var DotaPix = image.GetPixelData(0, xP);//取第一行
                    for (int Vy = 0; Vy < expandPix; Vy++)
                    {

                        destImage.SetPixelData(Vy, xP, DotaPix);
                    }
                }

            }
            else if (LocalDirection.Bottom == expandDir)
            {
                ///合并图案
                imageSrc.X = 0;
                imageSrc.Y = 0;
                CombineImage(image, destImage, imageSrc);
                //此时expandMM为高度
                //destImage图像的下面是复制出的图像//宽度0-imageWidth高度expandMM
                //复制元数据为最后行颜色
                for (int xP = 0; xP < image.Width; xP++)
                {

                    //var DotaPix1 = image.GetPixelColor(image.Height - 1, xP);
                    var DotaPix = image.GetPixelData(image.Height - 1, xP);
                    for (int Vy = image.Height; Vy < image.Height + expandPix; Vy++)
                    {

                        destImage.SetPixelData(Vy, xP, DotaPix);
                    }
                }



            }
            else if (LocalDirection.Left == expandDir)
            {
                ///合并图案
                imageSrc.X = expandPix;
                imageSrc.Y = 0;
                CombineImage(image, destImage, imageSrc);
                //此时expandMM为宽度
                //destImage图像的左边是复制出的图像
                //复制元数据为第一列颜色
                for (int row = 0; row < image.Height; row++)
                {
                    var DotaPix = image.GetPixelData(row, 0);
                    for (int col = 0; col < expandPix; col++)
                    {
                        destImage.SetPixelData(row, col, DotaPix);
                    }
                }


            }
            else if (LocalDirection.Right == expandDir)
            {///合并图案
                imageSrc.X = 0; imageSrc.Y = 0;
                CombineImage(image, destImage, imageSrc);
                //此时expandMM为宽度
                //destImage图像的右边边是复制出的图像
                //复制元数据为最后列颜色
                for (int row = 0; row < image.Height; row++)
                {
                    var DotaPix = image.GetPixelData(row, image.Width - 1);
                    for (int col = image.Width; col < image.Width + expandPix; col++)
                    {
                        destImage.SetPixelData(row, col, DotaPix);
                    }
                }

            }
            else
            {
                //此时expandMM为高度
                //destImage图像的上面是复制出的图像
                //宽度0-imageWidth高度expandMM
                //复制元数据为第一行颜色
                for (int col = 0; col < expandPix; col++)
                {
                    var DotaPix = image.GetPixelData(0, col);
                    ///列
                    for (int row = 0; row < imageWidth; row++)
                    {
                        //行

                        destImage.SetPixelData(row, col, DotaPix);
                    }
                }
                imageSrc.X = 0;
                imageSrc.Y = expandMM;
            }
            //codes.Save(destImage, @"D:\test2.jpg", RasterImageFormat.Jpeg, 24);




            if (footLine)
            {

            }

            return destImage;

        }
        /// <summary>
        /// 扩充带颜色的边
        /// </summary>
        /// <param name="image">图像</param>
        /// <param name="expandMM">扩边大小</param>
        /// <param name="expandDir">扩边方向</param>
        /// <param name="color">扩边底色</param>
        /// <param name="dpi">分辨率</param>
        /// <param name="footLine">是否话脚线</param>
        /// <returns></returns>
        public RasterImage ExpandImageColor(RasterImage image, int expandMM, LocalDirection expandDir, Color color, int dpi = Dpi, bool footLine = false)
        {
            RasterImage destImage = null;
            Size imageSize = new Size(0, 0);
            Rectangle ImageRc = new Rectangle(0, 0, 0, 0);
            if (expandDir == LocalDirection.Bottom || expandDir == LocalDirection.Top)
            {
                imageSize = new Size(image.Width, image.Height + ConvertEx.Mm2Pix(expandMM, dpi));
                ImageRc = new Rectangle(0, 0, imageSize.Width, imageSize.Height);
            }
            else if (expandDir == LocalDirection.Left || expandDir == LocalDirection.Right)
            {
                imageSize = new Size(image.Width + ConvertEx.Mm2Pix(expandMM, dpi), image.Height);
                ImageRc = new Rectangle(0, 0, imageSize.Width, imageSize.Height);
            }
            destImage = CreateRasterImage(imageSize, dpi, color.R, color.G, color.B);

            CombineImage(image, destImage, ImageRc);
            DrewAllFootLine(image);
            return destImage;

        }
        //private void DrewFootLine(RasterImage srcImage,)
        /// <summary>
        /// 吧srcImage合并到destImage上面去
        /// </summary>srcImage
        /// <param name="srcImage"></param>
        /// <param name="destImage"></param>
        /// <param name="rc"></param>
        public void CombineImage(RasterImage srcImage, RasterImage destImage, System.Drawing.Rectangle rc)
        {
            try
            {
                CombineFastCommand cfctop = new CombineFastCommand();
                //cfctop.DestinationRectangle = new Rectangle(180, 0, boardPointInfoImage.Width, boardPointInfoImage.Height);
                cfctop.DestinationRectangle = rc;
                cfctop.SourcePoint = Point.Empty;
                cfctop.Flags = CombineFastCommandFlags.OperationAdd | CombineFastCommandFlags.Destination0;
                cfctop.DestinationImage = destImage;
                cfctop.Run(srcImage);
            }
            catch (Exception)
            {

                throw;
            }
        }
        //private void DrewFootLine(RasterImage srcImage,)
        /// <summary>
        /// 吧srcImage合并到destImage上面去
        /// </summary>srcImage
        /// <param name="srcImage"></param>
        /// <param name="destImage"></param>
        /// <param name="rc"></param>
        public void CombineImage(RasterImage srcImage, RasterImage destImage,Point pi)
        {
            try
            {
                CombineFastCommand cfctop = new CombineFastCommand();
                Rectangle rc = new Rectangle(pi.X, pi.Y, srcImage.Width, srcImage.Height);
                cfctop.DestinationRectangle = rc;
                cfctop.SourcePoint = Point.Empty;
                cfctop.Flags = CombineFastCommandFlags.OperationAdd | CombineFastCommandFlags.Destination0;
                cfctop.DestinationImage = destImage;
                cfctop.Run(srcImage);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        ///从srcImage裁切出图
        /// </summary>
        /// <param name="srcImage"></param>
        /// <param name="rc"></param>
        public void CropRasterImage(RasterImage srcImage, Rectangle rc)
        {
            IRasterCommand cropcommand = new CropCommand(rc);
            cropcommand.Run(srcImage);
        }

        protected RasterImage CombineTwoImage(RasterImage srcImage1, RasterImage srcImage2, Direction dictDirection = Direction.Horizontal, int dpi = Dpi)
        {
            RasterImage destImage = null;
            ;
            try
            {
                if (dictDirection == Direction.Horizontal)
                {
                    destImage = new RasterImage(
                        RasterMemoryFlags.Tiled,
                        srcImage1.Width + srcImage2.Width,
                        srcImage1.Height,
                        24,
                        RasterByteOrder.Bgr,
                        RasterViewPerspective.BottomLeft,
                        null,
                        IntPtr.Zero,
                        0);
                    destImage.XResolution = destImage.YResolution = dpi;
                    System.Drawing.Rectangle rc1 = new Rectangle(0, 0, srcImage1.Width, srcImage1.Height);
                    System.Drawing.Rectangle rc2 = new Rectangle(srcImage1.Width, 0, srcImage2.Width, srcImage2.Height);
                    CombineFastCommand cfctop = new CombineFastCommand();
                    //cfctop.DestinationRectangle = new Rectangle(180, 0, boardPointInfoImage.Width, boardPointInfoImage.Height);
                    destImage.XResolution = srcImage1.XResolution;
                    destImage.YResolution = srcImage1.YResolution;
                    cfctop.SourcePoint = Point.Empty;
                    cfctop.Flags = CombineFastCommandFlags.OperationAdd | CombineFastCommandFlags.Destination0;
                    cfctop.DestinationImage = destImage;
                    cfctop.DestinationRectangle = rc1;
                    cfctop.Run(srcImage1);
                    cfctop.DestinationRectangle = rc2;
                    cfctop.Run(srcImage2);
                }
                else
                {
                    destImage = new RasterImage(
                        RasterMemoryFlags.Tiled,
                        srcImage1.Width,
                        srcImage1.Height + srcImage2.Height,
                        24,
                        RasterByteOrder.Bgr,
                        RasterViewPerspective.BottomLeft,
                        null,
                        IntPtr.Zero,
                        0);
                    destImage.XResolution = destImage.YResolution = dpi;
                    System.Drawing.Rectangle rc1 = new Rectangle(0, 0, srcImage1.Width, srcImage1.Height);
                    System.Drawing.Rectangle rc2 = new Rectangle(0, srcImage1.Height, srcImage2.Width, srcImage2.Height);
                    CombineFastCommand cfctop = new CombineFastCommand();
                    //cfctop.DestinationRectangle = new Rectangle(180, 0, boardPointInfoImage.Width, boardPointInfoImage.Height);
                    destImage.XResolution = srcImage1.XResolution;
                    destImage.YResolution = srcImage1.YResolution;
                    cfctop.SourcePoint = Point.Empty;
                    cfctop.Flags = CombineFastCommandFlags.OperationAdd | CombineFastCommandFlags.Destination0;
                    cfctop.DestinationImage = destImage;
                    cfctop.DestinationRectangle = rc1;
                    cfctop.Run(srcImage1);
                    cfctop.DestinationRectangle = rc2;
                    cfctop.Run(srcImage2);
                }
                return destImage;

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected RasterImage CreateColorBlack(int inNum, Size inSize)
        {
            int index = inNum%50;
            var color = _colorList[index];
            return CreateRasterImage(inSize, color.R, color.G, color.B);
        }

        /// <summary>
        /// 创建空图，size像素值
        /// </summary>
        /// <param name="size"></param>
        /// <param name="dpi"></param>
        /// <param name="inR"></param>
        /// <param name="inG"></param>
        /// <param name="inB"></param>
        /// <returns></returns>
        protected RasterImage CreateRasterImage(Size size, int dpi = Dpi, int inR = 255, int inG = 255, int inB = 255)
        {

            try
            {
                RasterImage destImage = new RasterImage(
                                 RasterMemoryFlags.Conventional,
                                    size.Width,
                                     size.Height,
                                     24,
                                     RasterByteOrder.Bgr,
                                     RasterViewPerspective.BottomLeft,
                                     null,
                                     IntPtr.Zero,
                                     0);
                destImage.XResolution = destImage.YResolution = dpi;
                destImage.OriginalFormat = RasterImageFormat.Jpeg;
                FillCommand fcommand = new FillCommand();

                fcommand.Color = new RasterColor(inR, inG, inB);
                fcommand.Run(destImage);

                return destImage;

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 裁切尺寸单位像素
        /// </summary>
        /// <param name="srcImage"></param>
        /// <param name="size"></param>
        protected void ResizeImage(RasterImage srcImage, Size size)
        {
            if (srcImage.Width == size.Width && srcImage.Height == size.Height)
            {
                return;
            }
            SizeCommand command = new SizeCommand();

            command.Height = size.Height;
            command.Width = size.Width;
            command.Flags = RasterSizeFlags.Bicubic;
            command.Run(srcImage);
        }
        /// <summary>
        /// 等分照片
        /// </summary>
        /// <param name="image">原照片</param>
        /// <param name="splitDirection">等分方式</param>
        /// <param name="splitNum"></param>
        /// <param name="codes"></param>
        /// <returns></returns>
        public List<RasterImage> SplitRasterImage(RasterImage image, Direction splitDirection, int splitNum = 2, RasterCodecs codes = null)
        {
            List<RasterImage> rtnList = new List<RasterImage>();
            Image gdiImage;
            try
            {
                gdiImage = image.ConvertToGdiPlusImage();
            }
            catch (Exception)
            {
                string filepath = Path.Combine(Path.GetTempPath(), (image.ImageSize.Width * image.ImageSize.Height).ToString(), ".jpg");
                if (File.Exists(filepath))
                {
                    File.Delete(filepath);
                }
                codes.Save(image, filepath, RasterImageFormat.Jpeg, 24);
                gdiImage = Image.FromFile(filepath);
            }
            if (splitDirection == Direction.Horizontal)
            {

                int iHeight = image.Height / splitNum;//等分后的高度
                for (int partNum = 0; partNum < splitNum; partNum++)
                {
                    int partX = 0;
                    int partY = iHeight * partNum;
                    int partW = image.Width;
                    int partH = iHeight;

                    using (Image im = GetPart(gdiImage, 0, 0, partW, partH, partX, partY, image.BitsPerPixel))
                    {
                        RasterImage rasterIm = new RasterImage(im);
                        rasterIm.XResolution = image.XResolution;
                        rasterIm.YResolution = image.YResolution;
                        rtnList.Add(rasterIm);
                        im.Dispose();
                    }
                }
            }
            else if (splitDirection == Direction.Vertical)
            {
                int iWidth = image.Width / splitNum;//等分后的高度
                int iHeight = image.Height;
                Bitmap partImg1 = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
                Bitmap partImg2 = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
                Rectangle destRect = new Rectangle(new Point(0, 0),
                 new Size(iWidth, iHeight)); //目标位置
                Rectangle origRect1 = new Rectangle(new Point(0, 0),
                    new Size(iWidth, iHeight)); //原图位置（默认从原图中截取的图片大小等于目标图片的大小）
                Rectangle origRect2 = new Rectangle(new Point(iWidth, 0),
                   new Size(iWidth, iHeight)); //原图位置（默认从原图中截取的图片大小等于目标图片的大小）
                using (Graphics graphics = Graphics.FromImage(partImg1))
                {

                    try
                    {
                        graphics.DrawImage(gdiImage, destRect, origRect1, GraphicsUnit.Pixel);
                        RasterImage rasterIm = new RasterImage(partImg1);
                        rasterIm.XResolution = image.XResolution;
                        rasterIm.YResolution = image.YResolution;
                        rtnList.Add(rasterIm);
                        partImg1.Dispose();
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }
                using (Graphics graphics = Graphics.FromImage(partImg2))
                {


                    try
                    {


                        graphics.DrawImage(gdiImage, destRect, origRect2, GraphicsUnit.Pixel);
                        RasterImage rasterIm = new RasterImage(partImg2);
                        rasterIm.XResolution = image.XResolution;
                        rasterIm.YResolution = image.YResolution;
                        rtnList.Add(rasterIm);
                        partImg2.Dispose();
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }
                //for (int partNum = 0; partNum < splitNum; partNum++)
                //{
                //    int partX = iWidth * partNum;
                //    int partY = 0;
                //    int partW = iWidth;
                //    int partH = image.Height;
                //    using (Image im = GetPart(gdiImage, 0, 0, partW, partH, partX, partY, image.BitsPerPixel))
                //    {
                //        RasterImage rasterIm = new RasterImage(im);
                //        rasterIm.XResolution = image.XResolution;
                //        rasterIm.YResolution = image.YResolution;
                //        rtnList.Add(rasterIm);
                //        im.Dispose();
                //    }                  

                //}
            }
            // 测试代码
            //int icount = 0;
            //foreach (RasterImage rasterImage in rtnList)
            //{

            //    codes.Save(rasterImage, @"D:\test" + icount + ".jpg", RasterImageFormat.Jpeg, 24);
            //    icount++;
            //}
            gdiImage = null;
            return rtnList;
        }
        public List<RasterImage> SplitRasterImageAndLine(RasterImage image, Direction splitDirection, int splitNum = 2, int expandMm = 12, int footLineMm = 3, RasterCodecs codes = null)
        {
            const FootLineDirection footLineFlags = FootLineDirection.LeftTop | FootLineDirection.RightTop |
                                                    FootLineDirection.LeftBottom | FootLineDirection.RightBottom;
            int expandPix = (int)(expandMm / 25.4f * image.XResolution);
            List<RasterImage> rtnList = new List<RasterImage>();
            if (splitDirection == Direction.Horizontal)
            {

                int iHeight = image.Height / splitNum;//等分后的高度
                for (int partNum = 0; partNum < splitNum; partNum++)
                {
                    int partX = 0;
                    int partY = iHeight * partNum;
                    int partW = image.Width;
                    int partH = iHeight;
                    partH += expandPix;
                    if (partNum % 2 == 0)
                    {
                        RasterImage im1 = image.Clone(new Rectangle(partX, partY, partW, partH));

                        DrewFootLine(im1, footLineFlags, footLineMm);
                        rtnList.Add(im1);
                    }
                    else if (partNum % 2 == 1)
                    {
                        partY -= expandPix;
                        partY = partY >= 0 ? partY : 0;
                        RasterImage im1 = image.Clone(new Rectangle(partX, partY, partW, partH));
                        DrewFootLine(im1, footLineFlags, footLineMm);
                        //RotateImage(im1,180);
                        rtnList.Add(im1);
                    }


                }
            }
            else if (splitDirection == Direction.Vertical)
            {
                int iWidth = image.Width / splitNum;//等分后的宽度
                int iHeight = image.Height;

                for (int partNum = 0; partNum < splitNum; partNum++)
                {
                    int partX = iWidth * partNum;
                    int partY = 0;
                    int partW = iWidth;
                    int partH = iHeight;
                    partW += expandPix;
                    if (partNum % 2 == 0)
                    {
                        RasterImage im1 = image.Clone(new Rectangle(partX, partY, partW, partH));
                        DrewFootLine(im1, footLineFlags, footLineMm);
                        rtnList.Add(im1);
                    }
                    else if (partNum % 2 == 1)
                    {
                        partX -= expandPix;
                        partX = partX >= 0 ? partX : 0;
                        RasterImage im1 = image.Clone(new Rectangle(partX, partY, partW, partH));
                        DrewFootLine(im1, footLineFlags, footLineMm);
                        rtnList.Add(im1);
                    }

                }

            }

            return rtnList;
        }
        /// <summary>
        /// 画脚线
        /// </summary>
        /// <param name="image"></param>
        /// <param name="footLineDirection"></param>
        /// <param name="lineLengthMM"></param>
        public void DrewFootLine(RasterImage image, FootLineDirection footLineDirection, int lineLengthMM = 3)
        {
            int imageWigth = image.Width;
            int imageHeigth = image.Height;
            int linePix = ConvertEx.Mm2Pix(lineLengthMM, 254);
            using (RasterImageGdiPlusGraphicsContainer container = image.CreateGdiPlusGraphics())
            {
                using (Pen pen = new Pen(Color.Black, 2))
                {

                    container.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    if (footLineDirection.HasFlag(FootLineDirection.LeftTop))
                    {

                        container.Graphics.DrawLine(pen, new Point(linePix, 0), new Point(0, 0));
                        container.Graphics.DrawLine(pen, new Point(0, linePix), new Point(0, 0));
                    }

                    if (footLineDirection.HasFlag(FootLineDirection.LeftBottom))
                    {
                        container.Graphics.DrawLine(pen, new Point(0, imageHeigth - linePix), new Point(0, imageHeigth - 1));
                        container.Graphics.DrawLine(pen, new Point(linePix, imageHeigth - 1), new Point(0, imageHeigth - 1));
                    }
                    if (footLineDirection.HasFlag(FootLineDirection.RightTop))
                    {
                        container.Graphics.DrawLine(pen, new Point(imageWigth - linePix, 0), new Point(imageWigth - 1, 0));
                        container.Graphics.DrawLine(pen, new Point(imageWigth - 1, linePix), new Point(imageWigth - 1, 0));
                    }
                    if (footLineDirection.HasFlag(FootLineDirection.RightBottom))
                    {
                        container.Graphics.DrawLine(pen, new Point(imageWigth - linePix, imageHeigth), new Point(imageWigth - 1, imageHeigth - 1));
                        container.Graphics.DrawLine(pen, new Point(imageWigth - 1, imageHeigth - linePix), new Point(imageWigth - 1, imageHeigth - 1));
                    }
                    if (footLineDirection.HasFlag(FootLineDirection.MiddleBoth))
                    {
                        container.Graphics.DrawLine(pen, new Point(imageWigth / 2, 0), new Point(imageWigth / 2, linePix));
                        container.Graphics.DrawLine(pen, new Point(imageWigth / 2, imageHeigth - linePix), new Point(imageWigth / 2, imageHeigth));
                    }
                }
            }
        }
        public void DrewCrossLine(RasterImage image, Point middlePoint, int lineLengthMM = 8)
        {
            int imageWigthPix = image.Width;
            int imageHeigthPix = image.Height;
            int lineLengthPix = ConvertEx.Mm2Pix(lineLengthMM, Dpi);//线段长度8mm

            using (RasterImageGdiPlusGraphicsContainer container = image.CreateGdiPlusGraphics())
            {
                using (Pen pen = new Pen(Color.Black, 2))
                {

                    container.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

                    //左上角
                    container.Graphics.DrawLine(pen, new Point(middlePoint.X - lineLengthPix / 2, middlePoint.Y), new Point(middlePoint.X + lineLengthPix / 2, middlePoint.Y));
                    container.Graphics.DrawLine(pen, new Point(middlePoint.X, middlePoint.Y - lineLengthPix / 2), new Point(middlePoint.X, middlePoint.Y + lineLengthPix / 2));



                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="image">图案</param>
        /// <param name="lineLengthMM">脚线长度</param>
        /// <param name="apexMergenMM">脚线距离四个顶点的距离</param>
        /// <param name="middleMM">中线距离书脊宽度</param>
        /// <param name="expendMM">宽度书脊</param>
        public void DrewFaceLine(RasterImage image, int lineLengthMM = 8, int apexMergenMM = 25, int middleMM = 10, int expendMM = 10)
        {
            int imageWigthPix = image.Width;
            int imageHeigthPix = image.Height;
            int lineLengthPix = ConvertEx.Mm2Pix(lineLengthMM, Dpi);//线段长度8mm
            int mergenPix = ConvertEx.Mm2Pix(apexMergenMM, Dpi);//距离边25毫米
            int expendPix = ConvertEx.Mm2Pix(expendMM, Dpi);//中间书脊宽度
            int middlePix = ConvertEx.Mm2Pix(middleMM, Dpi);//脚线距离书脊长度
            using (RasterImageGdiPlusGraphicsContainer container = image.CreateGdiPlusGraphics())
            {
                using (Pen pen = new Pen(Color.Black, 2))
                {

                    container.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

                    //左上角
                    container.Graphics.DrawLine(pen, new Point(mergenPix, 0), new Point(mergenPix, lineLengthPix));
                    container.Graphics.DrawLine(pen, new Point(0, mergenPix), new Point(lineLengthPix, mergenPix));

                    //上中线
                    int middleX = imageWigthPix / 2;
                    int leftMiddleLineX = middleX - expendPix / 2 - middlePix;
                    int rightMiddleLineX = middleX + expendPix / 2 + middlePix;
                    container.Graphics.DrawLine(pen, new Point(leftMiddleLineX, 0), new Point(leftMiddleLineX, lineLengthPix));
                    container.Graphics.DrawLine(pen, new Point(rightMiddleLineX, 0), new Point(rightMiddleLineX, lineLengthPix));
                    //上书脊线
                    int leftExpendLineX = middleX - expendPix / 2;
                    int rightExpendLineX = middleX + expendPix / 2;
                    container.Graphics.DrawLine(pen, new Point(leftExpendLineX, 0), new Point(leftExpendLineX, lineLengthPix));
                    container.Graphics.DrawLine(pen, new Point(rightExpendLineX, 0), new Point(rightExpendLineX, lineLengthPix));

                    ///右上角                    
                    container.Graphics.DrawLine(pen, new Point(imageWigthPix - mergenPix, 0), new Point(imageWigthPix - mergenPix, lineLengthPix));
                    container.Graphics.DrawLine(pen, new Point(imageWigthPix - lineLengthPix, mergenPix), new Point(imageWigthPix, mergenPix));

                    //左下角
                    container.Graphics.DrawLine(pen, new Point(0, imageHeigthPix - mergenPix), new Point(lineLengthPix, imageHeigthPix - mergenPix));
                    container.Graphics.DrawLine(pen, new Point(mergenPix, imageHeigthPix), new Point(mergenPix, imageHeigthPix - lineLengthPix));

                    //下中线
                    container.Graphics.DrawLine(pen, new Point(leftExpendLineX, imageHeigthPix - lineLengthPix), new Point(leftExpendLineX, imageHeigthPix));
                    container.Graphics.DrawLine(pen, new Point(rightMiddleLineX, imageHeigthPix - lineLengthPix), new Point(rightMiddleLineX, imageHeigthPix));
                    //下书脊线
                    container.Graphics.DrawLine(pen, new Point(leftMiddleLineX, imageHeigthPix - lineLengthPix), new Point(leftMiddleLineX, imageHeigthPix));
                    container.Graphics.DrawLine(pen, new Point(rightExpendLineX, imageHeigthPix - lineLengthPix), new Point(rightExpendLineX, imageHeigthPix));

                    //右下
                    container.Graphics.DrawLine(pen, new Point(imageWigthPix - mergenPix, imageHeigthPix), new Point(imageWigthPix - mergenPix, imageHeigthPix - lineLengthPix));
                    container.Graphics.DrawLine(pen, new Point(imageWigthPix - lineLengthPix, imageHeigthPix - mergenPix), new Point(imageWigthPix, imageHeigthPix - mergenPix));

                }
            }
        }
        /// 获取指定区域图片
        /// </summary>
        /// <param name="pPath">图片路径</param>
        /// <param name="pSavePath">保存路径</param>
        /// <param name="pPartStartPointX">目标图片开始绘制处的坐标X值(通常为)</param>
        /// <param name="pPartStartPointY">目标图片开始绘制处的坐标Y值(通常为)</param>
        /// <param name="pPartWidth">目标图片的宽度</param>
        /// <param name="pPartHeight">目标图片的高度</param>
        /// <param name="pOrigStartPointX">原始图片开始截取处的坐标X值</param>
        /// <param name="pOrigStartPointY">原始图片开始截取处的坐标Y值</param>
        /// <param name="pFormat">保存格式，通常可以是jpeg</param>
        public Image GetPart(Image image, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY, int bitPix = 24)
        {
            Image originalImg = image;
            PixelFormat pixelFormat = PixelFormat.Format24bppRgb;
            if (bitPix == 24)
            {
                pixelFormat = PixelFormat.Format24bppRgb;
            }
            else if (bitPix == 32)
            {
                pixelFormat = PixelFormat.Format32bppArgb;
            }

            Bitmap partImg = new Bitmap(pPartWidth, pPartHeight, pixelFormat);
            using (Graphics graphics = Graphics.FromImage(partImg))
            {
                Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY),
                    new Size(pPartWidth, pPartHeight)); //目标位置
                Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY),
                    new Size(pPartWidth, pPartHeight)); //原图位置（默认从原图中截取的图片大小等于目标图片的大小）
                try
                {
                    graphics.DrawImage(originalImg, destRect, origRect, GraphicsUnit.Pixel);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            //partImg.Save(pSavedPath + "\\part.jpg", ImageFormat.Jpeg);

            return partImg;
        }


        public void SaveRasteImage(RasterCodecs codes, RasterImage image, string filePath)
        {

            codes.Save(image, filePath, RasterImageFormat.Jpeg, 24);
        }

        protected void DrewAllFootLine(RasterImage image, int lineLengthMM = 3)
        {
            DrewFootLine(image, FootLineDirection.FixedPoint, lineLengthMM);
        }



        public bool ThumbnailCallback()
        {
            return false;
        }
        /// <summary>
        /// 旋转图片
        /// </summary>
        /// <param name="image"></param>
        /// <param name="angle"></param>
        protected void RotateImage(RasterImage image, int angle)
        {
            //smallPicImage.c
            RotateCommand rc = new RotateCommand();
            rc.Angle = angle * 100;
            rc.FillColor = new RasterColor(255, 255, 255);
            rc.Flags = RotateCommandFlags.Resize | RotateCommandFlags.Bicubic;
            rc.Run(image);
        }

        protected void ChangeColorModel(RasterImage image)
        {
            ColorResolutionCommand colorRes = new ColorResolutionCommand();
            colorRes.Mode = ColorResolutionCommandMode.InPlace;
            colorRes.BitsPerPixel = 24;
            colorRes.DitheringMethod = RasterDitheringMethod.None;
            colorRes.PaletteFlags = ColorResolutionCommandPaletteFlags.Fixed;
            colorRes.Colors = 0;
            colorRes.Run(image);
        }

        /// <summary>
        /// 生成条码
        /// </summary>
        /// <param name="inOrderContent">订单内容逗号隔开</param>
        /// <param name="inPhotoContent">照片名</param>
        /// <param name="inWidth">宽度</param>
        /// <param name="inHeightMm">高度</param>
        /// <param name="inDpi">分辨率</param>
        /// <param name="isVertical">是否竖向</param>
        /// <param name="barcodeFont">条码字体</param>
        /// <param name="textFont">备注字体</param>
        /// <returns></returns>
        protected RasterImage gDrawGenerateTags(string inOrderContent, string inPhotoContent, int inWidthPix, int inHeightMm = 5, int inDpi = 254, bool isVertical = true, Font barcodeFont = null, Font textFont = null, int offset = 0)
        {
            if (string.IsNullOrEmpty(inOrderContent))
            {
                inOrderContent = "1234,测试产品";
            }
            if (barcodeFont == null)
            {
                barcodeFont = new Font("3 of 9 Barcode", 20);// new Font("微软雅黑",24))

            }
            if (textFont == null)
            {
                textFont = new Font("微软雅黑", 12);
            }

            int currXLocal = ConvertEx.Mm2Pix(offset, 254);//当前操作在备注栏上的相对位置
            //Font barcodeFont
            RasterImage destImage = null;
            string orderproductId = inOrderContent.Split('_')[0];
            RasterColor firstColor = new RasterColor(0, 0, 0);
            RasterColor secondColor = new RasterColor(0, 0, 0);

            int pixHeight = ConvertEx.Mm2Pix(inHeightMm, inDpi);
            int pixWidth = inWidthPix;
            int conLength = orderproductId.Length;
            destImage = CreateRasterImage(new Size(pixWidth, pixHeight));

            if (conLength >= 4)
            {
                string fistCom = orderproductId.Substring(conLength - 4, 1) + orderproductId.Substring(conLength - 2, 1);
                string secondCom = orderproductId.Substring(conLength - 3, 1) + orderproductId.Substring(conLength - 1, 1);
                int intFirst = Convert.ToInt32(fistCom);
                int intSecond = Convert.ToInt32(secondCom);
                int intFirstColor = intFirst % _colorList.Count;
                int intSecondColor = intSecond % _colorList.Count;
                firstColor = _colorList[intFirstColor];
                secondColor = _colorList[intSecondColor];
            }
            // string code128 = BarcodeConverter.ConvertToCode128(orderproductId);            \
            string barcode = "*" + orderproductId + "*";
            RasterImage firstColorImage = CreateRasterImage(new Size(ColorWidth, pixHeight), firstColor.R, firstColor.G,
                firstColor.B);
            RasterImage secondColorImage = CreateRasterImage(new Size(ColorWidth, pixHeight), secondColor.R, secondColor.G,
               secondColor.B);
            Graphics gDraw = destImage.CreateGdiPlusGraphics().Graphics;//用来打印条码和文字
            Rectangle fcRectangle = new Rectangle(currXLocal, 0, ColorWidth, pixHeight);
            currXLocal += ColorWidth;
            Rectangle scRectangle = new Rectangle(currXLocal, 0, ColorWidth, pixHeight);
            currXLocal += ColorWidth;
            //SpecialEffectsProcessor processor = new SpecialEffectsProcessor();
            currXLocal += 15;
            SizeF sizeF = gDraw.MeasureString(barcode, barcodeFont);
            int BARCODEWIDTH = Convert.ToInt32(sizeF.Width / gDraw.DpiX * inDpi) + 2;//条码的宽度
            Rectangle rcBarcode = new Rectangle(currXLocal, 0, BARCODEWIDTH, pixHeight);
            currXLocal += BARCODEWIDTH;
            currXLocal += 5;
            Rectangle rcFont = new Rectangle(currXLocal, -5, destImage.Width - currXLocal, pixHeight);
            gDraw.DrawString(barcode, barcodeFont, Brushes.Black, rcBarcode.X, rcBarcode.Y);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Near;
            sf.LineAlignment = StringAlignment.Near;

            gDraw.DrawString(inPhotoContent + inOrderContent, textFont, new SolidBrush(Color.Black), rcFont, sf);
            ColorResolutionCommand command = new ColorResolutionCommand(ColorResolutionCommandMode.InPlace, 24, RasterByteOrder.Bgr,
                                                               RasterDitheringMethod.None, ColorResolutionCommandPaletteFlags.Optimized, null);
            command.Run(destImage);//处理一遍条码和文字

            CombineImage(firstColorImage, destImage, fcRectangle);

            CombineImage(secondColorImage, destImage, scRectangle);
            if (isVertical)
            {
                RotateImage(destImage, 90);
            }

            return destImage;
        }

        protected void DrewText(RasterImage destImage, string text, Font font, Font smallFont, Rectangle rectangle, int smallPhotosize = 6)
        {
            Rectangle rcFont = rectangle;
            Graphics gText = destImage.CreateGdiPlusGraphics().Graphics;//用来打印条码和文字
      
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Near;
            sf.LineAlignment = StringAlignment.Near;

            if (smallPhotosize > (float)destImage.Width / destImage.XResolution)
            {
                //StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Near;
                gText.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
                gText.DrawString(text, smallFont, new SolidBrush(Color.Black), rcFont, sf);
            }
            else
            {
               // StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Near;
                gText.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
                gText.DrawString(text, font, new SolidBrush(Color.Black), rcFont, sf);
            }
            //gDraw.DrawString(text,font, new SolidBrush(Color.Black), rcFont, sf);
            ColorResolutionCommand command = new ColorResolutionCommand(ColorResolutionCommandMode.InPlace, 24, RasterByteOrder.Bgr,
                                                               RasterDitheringMethod.None, ColorResolutionCommandPaletteFlags.Optimized, null);
               command.Run(destImage);//处理一遍条码和文字
        }

        protected LocalDirection GetLocalDirection(Size sbase, Size s1, int ExpandMm = 17)
        {
            LocalDirection remarkLocal;
            if (sbase.Height - s1.Height > ExpandMm)
            {
                remarkLocal = LocalDirection.Bottom;
            }
            else
            {
                if (sbase.Width - s1.Width >= 5)
                {
                    remarkLocal = LocalDirection.Top;
                }
                else
                {
                    remarkLocal = LocalDirection.None;

                }
            }
            return remarkLocal;
        }

        /// <summary>
        /// 在srcImage上添加barCodeImage位置在remarkLocal 返回新的image对象
        /// </summary>
        /// <param name="srcImage"></param>
        /// <param name="barCodeImage"></param>
        /// <param name="remarkLocal"></param>
        /// <param name="dpi"></param>
        /// <returns></returns>
        protected RasterImage CombineBarImage(RasterImage srcImage, RasterImage barCodeImage, LocalDirection remarkLocal = LocalDirection.Left, int dpi = Dpi)
        {

            CombineFastCommand cfctop = new CombineFastCommand();
            RasterImage destImage = null;
            Rectangle srcRectangle = new Rectangle(0, 0, 0, 0);
            Rectangle codeRectangle = new Rectangle(0, 0, 0, 0);



            if (remarkLocal == LocalDirection.Left || remarkLocal == LocalDirection.Right)
            {
                RotateImage(barCodeImage, 90);
                destImage = CreateRasterImage(new Size((srcImage.Width + barCodeImage.Width), srcImage.Height));
                destImage.XResolution = destImage.YResolution = dpi;
                srcRectangle = new Rectangle(barCodeImage.Width, 0, srcImage.Width, srcImage.Height);
                codeRectangle = new Rectangle(0, 0, barCodeImage.Width, barCodeImage.Height);
                cfctop.SourcePoint = Point.Empty;
                cfctop.Flags = CombineFastCommandFlags.OperationAdd | CombineFastCommandFlags.Destination0;
                cfctop.DestinationImage = destImage;
                cfctop.DestinationRectangle = srcRectangle;
                cfctop.Run(srcImage);
                cfctop.DestinationRectangle = codeRectangle;
                cfctop.Run(barCodeImage);
            }
            else if (remarkLocal == LocalDirection.Bottom || remarkLocal == LocalDirection.Top)
            {

                destImage = CreateRasterImage(new Size((srcImage.Width), srcImage.Height + barCodeImage.Height));

                destImage.XResolution = destImage.YResolution = dpi;
                srcRectangle = new Rectangle(0, 0, srcImage.Width, srcImage.Height);
                codeRectangle = new Rectangle(0, srcImage.Height, barCodeImage.Width, barCodeImage.Height);
                cfctop.SourcePoint = Point.Empty;
                cfctop.Flags = CombineFastCommandFlags.OperationAdd | CombineFastCommandFlags.Destination0;
                cfctop.DestinationImage = destImage;
                cfctop.DestinationRectangle = srcRectangle;
                cfctop.Run(srcImage);
                cfctop.DestinationRectangle = codeRectangle;
                cfctop.Run(barCodeImage);
            }

            return destImage;
        }



        protected RasterImage LoadPic(RasterCodecs codecs, string fmPath)
        {

            RasterImage rtnImage = codecs.Load(fmPath);

            int srcWPix = rtnImage.Width;
            int srcHPic = rtnImage.Height;
            int rsWPix = Convert.ToInt32(srcWPix / (float)rtnImage.XResolution * Dpi);
            int rsHPix = Convert.ToInt32(srcHPic / (float)rtnImage.YResolution * Dpi);
            rtnImage.XResolution = rtnImage.YResolution = Dpi;
            ResizeImage(rtnImage, new Size(rsWPix, rsHPix));
            return rtnImage;
        }

        protected bool ResizeImageRotate(RasterImage img, Size size)
        {
            //157,210
            bool isRotate = false;
            bool isStandhorizontal = false;
            bool isImagehorizontal = false;

            if (size.Width >= size.Height)
            {
                isStandhorizontal = true;
            }
            if (img.Width >= img.Height)
            {
                isImagehorizontal = true;
            }
            if (isStandhorizontal == !isImagehorizontal)
            {
                RotateImage(img, 90);
                isRotate = true;
            }

            ResizeImage(img, size);
            return isRotate;
        }

        protected RasterImage GenBarcode128(RasterCodecs codec, string code)
        {
            System.Drawing.Bitmap imgTemp = _code128.GetCodeImage(code, Code128.Encode.Code128A);
            string codetempPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + "BarCode.jpg";
            if (File.Exists(codetempPath))
            {
                File.Delete(codetempPath);
            }
            imgTemp.Save(codetempPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            RasterImage image = LoadPic(codec, codetempPath);
            return image;
        }
    }
}
